package com.example.mymusic;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;


public class MainActivity extends AppCompatActivity{
    private static final String TAG="MainActivity";
    //  获取控件，组件ID名字
    BridgeWebView webView = (BridgeWebView)findViewById(R.id.web_view);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //仅去掉标题栏，系统状态栏还是会显示
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

//        调用JS，两句不加会出现错误
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        //设置支持DomStorage
        webView.getSettings().setDomStorageEnabled(true);
//        访问网页
        webView.loadUrl("http://47.98.233.31");
        Log.d(TAG,"为什么没有声音");
//        系统默认会通过手机浏览器打开网页 为了能够直接通过WebView显示网页 必须设置
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url){
//                使用WebView加载显示url
                Log.d(TAG,"这就是webview吗，爱了爱了");
                view.loadUrl(url);
                return true;
            }
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Log.d(TAG,String.valueOf(errorCode));
                Log.d(TAG,description);

            }

        });
        webView.setWebChromeClient(new WebChromeClient(){
            public boolean onConsoleMessage(ConsoleMessage cm) {
                Log.d("MyApplication", cm.message() + " -- From line "
                        + cm.lineNumber() + " of "
                        + cm.sourceId() );
                return true;
            }
        });
        //注册Handler，供JS调用
        webView.registerHandler("submitFromWeb", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Log.i(TAG, "handler = submitFromWeb, data from web = " + data);
                function.onCallBack("submitFromWeb exe, response data from Java");
            }
        });
    }
}

